package view.purchasing;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.purchasing.OrderMst;
import utility.StringUtils;
import utility.com.TextComponentUtils;

public class OrderListTM extends AbstractTableModel {

    private List<OrderMst> data;

    public OrderListTM(List<OrderMst> data) {
        this.data = data;
    }

    public OrderListTM() {
        this(new ArrayList<OrderMst>(0));
    }

    public OrderMst get(int index) {
        return data.get(index);
    }

    public void add(OrderMst product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, OrderMst product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<OrderMst> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
         return 12;
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getTransNum();
            case 2: 
                if (data.get(rowIndex).getTransDate()!=null){
                    return StringUtils.formatDate(data.get(rowIndex).getTransDate());
                } return null;
                
            case 3: 
                if (data.get(rowIndex).getSupplier()!=null){
                    return data.get(rowIndex).getSupplier().getNama();
                } return null;
                
            case 4: return data.get(rowIndex).getLeadTime();
            case 5: return data.get(rowIndex).getAddTop() + data.get(rowIndex).getTop();
            case 6: return data.get(rowIndex).getDivisi();
            case 7: 
                 String displaySub = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getSubtotal(), 2);
                 return displaySub; 
            case 8: 
                 String displayDis = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getDiskon(),3);
                 return displayDis; 
            case 9: 
                 String displayTax = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getPpn(),2);
                 return displayTax; 
            case 10: 
                 String displayTot = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getTotal(),2);
                 return displayTot; 
        }
        return null;
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";
            case 1: return "NO ORDER";
            case 2: return "TGL ORDER";
            case 3: return "SUPPLIER";
            case 4: return "LTIME";
            case 5: return "TOP";
            case 6: return "DIV";
            case 7: return "SUBTOTAL";
            case 8: return "DISKON";
            case 9: return "PPN";
            case 10: return "TOTAL";
            default: return null;
        }
    }
    
}
